package gr.atc.epart;

import gr.atc.epart.actions.Action;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TabHost;

public class ActionsViewActivity extends ListActivity {

	private ArrayList<Action> _actions = null;
	private ActionsAdapter _adapter = null;
	List<Action> actions;
	ProgressDialog progressDialog;

	public void onCreate(Bundle savedInstanceState) {
		Log.v("ListActivity", "onCreate() called");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);

		_actions = new ArrayList<Action>();
		_adapter = new ActionsAdapter(this, R.layout.listviewrow, _actions);
		
		setListAdapter(_adapter);

		TabActivity parentActivity = (TabActivity) getParent();
		
		if (parentActivity == null) {
			actions = EPartVolatileDataStore.getInstance().getActionsList();
		} else {
			String tabtag = parentActivity.getTabHost().getCurrentTabTag();
			actions = EPartVolatileDataStore.getInstance().getActionsList(
					tabtag);
		}
		
		_actions.clear();
						
		if (actions != null) {

			for (Action action : actions)
				_actions.add(action);
			_adapter.notifyDataSetChanged();
		}
	}

	public void onResume() {
		super.onResume();
		Log.v("ListActivity", "onResume() called");
	TabActivity parentActivity = (TabActivity) getParent();
		
		if (parentActivity == null) {
			actions = EPartVolatileDataStore.getInstance().getActionsList();
		} else {
			String tabtag = parentActivity.getTabHost().getCurrentTabTag();
			actions = EPartVolatileDataStore.getInstance().getActionsList(
					tabtag);
		}
		
		_actions.clear();
						
		if (actions != null) {

			for (Action action : actions)
				_actions.add(action);
			_adapter.notifyDataSetChanged();
		}
	}

	public void onPause() {
		Log.v("ListActivity", "onPause() called");
		super.onPause();
	}

	public void onRestart() {
		Log.v("ListActivity", "onRestart() called");
		
		_adapter.notifyDataSetChanged();
		super.onRestart();

		// TabActivity parentActivity = (TabActivity) getParent();
		//
		// //_adapter.clear();
		// if (parentActivity == null) {
		// actions = EPartVolatileDataStore.getInstance().getActionsList();
		// } else {
		// String tabtag = parentActivity.getTabHost().getCurrentTabTag();
		// actions = EPartVolatileDataStore.getInstance().getActionsList(
		// tabtag);
		//
		// }
		// _actions.clear();
		//
		// if (actions != null) {
		//
		// for (Action action : actions)
		// _actions.add(action);
		//
		// _adapter.notifyDataSetChanged();
		// }

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
		Log.v("ListActivity", "onListItemClick() called");
		
		super.onListItemClick(l, v, position, id);
		
		showProgressDialog();
   
		
		FetchThread fetchThread = new FetchThread(position);
		fetchThread.start();
	
		

//		EPartVolatileDataStore.getInstance().setSelectedAction(
//				_actions.get(position));
//
//		try {
//			Intent actionDetailsIntent = new Intent();
//			actionDetailsIntent.setClassName("gr.atc.epart",
//					"gr.atc.epart.ActionDetailsActivity");
//			startActivity(actionDetailsIntent);
//		} catch (Exception e) {
//			Log.i("ActionsViewActivity", e.getMessage());
//		}

	}

	public void showProgressDialog() {
		
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("Please wait...");
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		progressDialog.show();
	}
	
	   // Define the Handler that receives messages from the thread and update the progress
    final Handler handler = new Handler() {
          public void handleMessage(Message msg) {
          progressDialog.dismiss();
        }
    };
    
    private class FetchThread extends Thread {
    	
    	int itemPosition;
    	
    	public FetchThread(int itemPosition) {
    		this.itemPosition = itemPosition;
    	}
    	
    	public void run() {
    		
    		EPartVolatileDataStore.getInstance().setSelectedAction(
    				_actions.get(itemPosition));

    		try {
    			Intent actionDetailsIntent = new Intent();
    			actionDetailsIntent.setClassName("gr.atc.epart",
    					"gr.atc.epart.ActionDetailsActivity");
    			startActivity(actionDetailsIntent);
    			handler.sendEmptyMessage(0);
    		} catch (Exception e) {
    			Log.i("ActionsViewActivity", e.getMessage());
    		}
    		
    	}    	
    	
    }


}
